package svc.elib.analysis;

import java.io.IOException;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedList;

import svc.elib.db.Author;
import svc.elib.db.Database;
import svc.elib.socnet.CentralityMetrics;
import svc.elib.socnet.EvolutionarySnapshots;
import svc.elib.socnet.Net;
import svc.elib.socnet.SocConstructor;

public class CompareTwoYears {
	
	private Net[] snaps;
	private int startYear;
	
	private class AuthorRanking implements Comparable<AuthorRanking> {
		public Author a;
		public double score;
		
		public AuthorRanking(Author a, double score) {
			this.a = a;
			this.score = score;
		}
		
		public String toString() {
			return a.getName() + ", " + score;
		}

		@Override
		public int compareTo(AuthorRanking o) {
			double diff = this.score - o.score;
			if (diff > 0)
				return -1;
			else
			if (diff < 0)
				return 1;
			else
				return 0;
		}
	}
	
	public CompareTwoYears(Net net) {
		EvolutionarySnapshots es = new EvolutionarySnapshots(net);
		snaps = es.getSnapshots();
		startYear = es.getStartYear();
	}
	
	private int yearToIndex(int year) {
		return year - startYear - 1;
	}
	
	public void compareDegree(int year) {
		Net current = snaps[yearToIndex(year)];
		Net prev = snaps[yearToIndex(year) - 1];
		
		Iterator<Author> ait = prev.getGraph().getVertices().iterator();
		LinkedList<AuthorRanking> prevList = new LinkedList<AuthorRanking>();
		LinkedList<AuthorRanking> currentList = new LinkedList<AuthorRanking>();
		
		while (ait.hasNext()) {
			Author a = ait.next();
			double b1 = prev.getGraph().degree(a);
			double b2 = current.getGraph().degree(a);
			prevList.add(new AuthorRanking(a, b1));
			currentList.add(new AuthorRanking(a, b2));
		}
		
		Collections.sort(prevList);
		Collections.sort(currentList);
		
		// determine top 5%
		int endIndex = (int) ((double) prevList.size() * 0.05);
	
		for (int j = 0; j < endIndex; j++) {
			System.out.println(prevList.get(j).toString() + "  ###  " + currentList.get(j).toString()); 
		}
	}
	
	public void compareBet(int year) {
		Net current = snaps[yearToIndex(year)];
		Net prev = snaps[yearToIndex(year) - 1];
		
		CentralityMetrics cmCurrent = new CentralityMetrics(current);
		cmCurrent.computeBetweeness(false);
		
		CentralityMetrics cmPrev = new CentralityMetrics(prev);
		cmPrev.computeBetweeness(false);
		
		Iterator<Author> ait = prev.getGraph().getVertices().iterator();
		LinkedList<AuthorRanking> prevList = new LinkedList<AuthorRanking>();
		LinkedList<AuthorRanking> currentList = new LinkedList<AuthorRanking>();
		
		while (ait.hasNext()) {
			Author a = ait.next();
			double b1 = cmPrev.getBetweenness(a.getName());
			double b2 = cmCurrent.getBetweenness(a.getName());
			prevList.add(new AuthorRanking(a, b1));
			currentList.add(new AuthorRanking(a, b2));
		}
		
		Collections.sort(prevList);
		Collections.sort(currentList);
		
		// determine top 5%
		int endIndex = (int) ((double) prevList.size() * 0.05);
	
		for (int j = 0; j < endIndex; j++) {
			System.out.println(prevList.get(j).toString() + "  ###  " + currentList.get(j).toString()); 
		}
	}
	
	public static void main(String[] args) 
		throws IOException
	{
		Database db = new Database("eLibData.csv", 1932, 2011);
		System.out.println(db.info());
		SocConstructor soc = new SocConstructor(db);
		Net net = soc.getNet();
		
		CompareTwoYears cty = new CompareTwoYears(net);
		cty.compareDegree(1961);
		//cty.compareDegree(1984);
		
		//cty.compareBet(1955);
		//cty.compareBet(2000);
		//cty.compareBet(2011);
		//cty.compareBet(1999);
	}

}
